package com.lucifer.cloud.boot.blog.service.impl;
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lucifer.cloud.boot.blog.domin.bo.Article;
import com.lucifer.cloud.boot.blog.domin.bo.Blog;
import com.lucifer.cloud.boot.blog.domin.bo.Exhibition;
import com.lucifer.cloud.boot.blog.domin.type.BlogConstant;
import com.lucifer.cloud.boot.blog.config.UserSystem;
import com.lucifer.cloud.boot.blog.domin.bo.Comment;
import com.lucifer.cloud.boot.blog.domin.bo.User;
import com.lucifer.cloud.boot.blog.domin.dto.comment.CommentConverter;
import com.lucifer.cloud.boot.blog.domin.dto.comment.CommentDto;
import com.lucifer.cloud.boot.blog.domin.type.BlogType;
import com.lucifer.cloud.boot.blog.mapper.ArticleMapper;
import com.lucifer.cloud.boot.blog.mapper.BlogMapper;
import com.lucifer.cloud.boot.blog.mapper.CommentMapper;
import com.lucifer.cloud.boot.blog.mapper.ExhibitionMapper;
import com.lucifer.cloud.boot.blog.mapper.UserMapper;
import com.lucifer.cloud.boot.blog.service.CommentService;
import com.lucifer.cloud.commons.model.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author lucifer
 * @date 2023/12/21 15:24
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private UserSystem userSystem;

    @Resource
    private UserMapper userMapper;

    @Resource
    private BlogMapper blogMapper;
    @Resource
    private ExhibitionMapper exhibitionMapper;

    @Resource
    private ArticleMapper articleMapper;


    @Override
    public CommentDto info(HttpServletRequest request,String content_id, Integer page, Integer limit, String type) {

        List<Comment> commentList = Lists.newArrayList();
        Long count = 0L;

        if(Objects.isNull(page) && Objects.isNull(limit)){
            commentList = list(Wrappers.lambdaQuery(Comment.class)
                    .eq(StringUtils.isNotBlank(content_id),Comment::getContent_id,content_id)
                    .eq(Comment::getType,type)
            );
            count = commentList.stream().count();
        }else {
            Page<Comment> rowPage = new Page<>(page, limit);
            Page<Comment> commentPage = this.baseMapper.selectPage(rowPage,Wrappers.lambdaQuery(Comment.class)
                    .eq(StringUtils.isNotBlank(content_id),Comment::getContent_id,content_id)
                    .eq(Comment::getType,type)

            );
            commentList = commentPage.getRecords();
            count = commentPage.getTotal();
        }

        List<Long> userIdList = Optional.ofNullable(commentList).orElse(Lists.newArrayList())
                    .stream().map(Comment::getUser_id)
                    .collect(Collectors.toList());

        List<User>  userList = userMapper.selectList(Wrappers.lambdaQuery(User.class).in(userIdList.size() > 0,User::getId, userIdList));

        return CommentConverter.converter2Dto(commentList,count,userList);
    }

    @Override
    public Result create(HttpServletRequest request, String contentId,String createdUserId, String content, String type) {
        Long userId = userSystem.userId(request);
        if(Objects.isNull(userId)){
            return Result.error(HttpStatus.UNAUTHORIZED.value(), BlogConstant.NOT_LOGIN);
        }
        Comment comment = CommentConverter.converterReq2Comment(contentId, content,createdUserId, type,userId);
        boolean save = save(comment);

        if(Objects.equals(BlogType.BLOG.getCode(),type)){
            blogMapper.update(new Blog(),Wrappers.lambdaUpdate(Blog.class)
                    .set(Blog::getUpdated_at, LocalDateTime.now())
                    .setSql("`comment`=`comment`+1")
                    .eq(Blog::getUid,contentId)
            );
        }

        if(Objects.equals(BlogType.IMAGE.getCode(),type)){
            exhibitionMapper.update(new Exhibition(),Wrappers.lambdaUpdate(Exhibition.class)
                    .set(Exhibition::getUpdated_at,LocalDateTime.now())
                    .setSql("`comment`=`comment`+1")
                    .eq(Exhibition::getUid,contentId)
            );
        }
        if(Objects.equals(BlogType.ARTICLE.getCode(),type)){
            articleMapper.update(new Article(),Wrappers.lambdaUpdate(Article.class)
                    .set(Article::getUpdated_at,LocalDateTime.now())
                    .setSql("`comment`=`comment`+1")
                    .eq(Article::getUid,contentId)
            );
        }

        return Result.success(save);
    }
}
